<?php
namespace app\admin\controller;



use app\common\model\Ueditor as UeditorModel;
use app\common\controller\AdminBase;
use think\Db;
use think\facade\Log;

class Tree extends AdminBase
{

    //树行菜单
    public function tree()
    {
        $cityid = input('pa', 0, 'intval');
        $schoolid = input('pb', 0, 'intval');

        $city = $this->getcity();
        return $this->fetch('tree', ['city'=>json_encode($city),'cityid'=>$cityid,'schoolid'=>$schoolid]);
    }

    public function getcity(){
        $swhere = "1=1";
        $plist = Db::name('citydata')->field('Id,name AS topic')->where('pid=0')->order('ord ASC,Id ASC')->select();

        if ( $plist ) {
            foreach( $plist as $pk=>$pv ) {
                $cdata =  Db::name('citydata')->field('Id,name AS topic')->where('pid='.$pv['Id'])->order('ord ASC,Id ASC')->select();
                if ( $cdata ) {
                    foreach( $cdata as $ck=>$cv ) {
                        $school = Db::name('school')->field('Id,schoolname AS topic,schoolcode')->where($swhere." AND cityid=".$cv['Id'])->select();
                        if ( $school ) {
                            foreach( $school as $sk=>$sv ) {
                                $school[$sk]['topic'] = $sv['schoolcode'].$sv['topic'];
                            }
                        }
                        $cdata[$ck]['childs'] = $school;
                    }
                }
                $plist[$pk]['childs'] = $cdata;
            }
        }
        return $plist;
    }




}